const { isDev } = require("../config");
const {
  pollDeliver,
  onPollBack,
  queryDeliver,
} = require("../service/third/kuaidi");

const app = require("express").Router();

const pollBackPath = "/kd100/poll-back";
function getPollBackUrl(req) {
  const origin = isDev ? "https://ice.qs3.fun" : req.origin;
  return origin + req.path0 + "/third" + pollBackPath;
}

app.all("/kd100/poll", async (req, res) => {
  const data = await pollDeliver(req._body, getPollBackUrl(req));
  res.send(data);
});

app.all(pollBackPath, async (req, res) => {
  const data = await onPollBack(req._body);
  res.send(data);
});

app.all("/kd100/query", async (req, res) => {
  const data = await queryDeliver(req._body);
  res.send(data);
});

module.exports = app;
